﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;

namespace School.Pages.Teaching
{
    public partial class TeacherSettings : System.Web.UI.Page
    {
        SchoolEntities db = new SchoolEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1500) == true)
                {
                    if (!IsPostBack)
                    {

                        GVTeachers.Visible = false;
                        SubjectsPanel.Visible = false;
                        DdlGrade.DataSource = db.ORG_Grade.Select(n => new { n.Id, n.Name });
                        DdlGrade.DataTextField = "Name";
                        DdlGrade.DataValueField = "Id";
                        DdlGrade.DataBind();

                        var grade = long.Parse(DdlGrade.SelectedValue);
                        DdlSubject.DataSource = db.TE_Subject.Where(n => n.Grad_Id == grade);
                        DdlSubject.DataTextField = "Subject_Name";
                        DdlSubject.DataValueField = "Id";
                        DdlSubject.DataBind();
                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        protected void GVTeachers_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var row = GVTeachers.Rows[int.Parse(e.CommandArgument.ToString())];
            var lbl = (Label)row.FindControl("LblId");

            HFTeacherId.Value = lbl.Text;
            GVTeachers.Visible = false;
            fillSubjectsGrid();
            SubjectsPanel.Visible = true;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SubjectsPanel.Visible = false;
            GVTeachers.Visible = true;
            LblNotFound.Visible = false;
            
            if (string.IsNullOrEmpty(TxtTeacherName.Text))
                return;

            var data = db.ORG_Stuff_Teacher.Select(x => new { Name = x.ORG_Stuff.FName + " " + x.ORG_Stuff.MName + " " + x.ORG_Stuff.LName, x.Id }).Where(x => x.Name.StartsWith(TxtTeacherName.Text));
            data = data.Union(db.ORG_Stuff_Teacher.Select(x => new { Name = x.ORG_Stuff.FName + " " + x.ORG_Stuff.MName + " " + x.ORG_Stuff.LName, x.Id }).Where(x => x.Name.Contains(TxtTeacherName.Text))).Distinct();
            if (data.Count() > 0)
            {
                GVTeachers.DataSource = data;
                GVTeachers.DataBind();
            }
            else
            {
                LblNotFound.Visible = true;
                GVTeachers.Visible = false;
            }
            TxtTeacherName.Text = "";
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            var tId = long.Parse(HFTeacherId.Value);
            var sId = int.Parse(DdlSubject.SelectedValue);
            db.AddToTe_Teacher_Subject(new Te_Teacher_Subject() { TeacherId = tId, SubjectId = sId });
            db.SaveChanges();
            fillSubjectsGrid();
        }

        private void fillSubjectsGrid()
        {
            var teacher = long.Parse(HFTeacherId.Value);
            var data = db.Te_Teacher_Subject.Where(x => x.TeacherId == teacher)
                .Select(x => new { Grade = x.TE_Subject.ORG_Grade.Name, Subject = x.TE_Subject.Subject_Name, x.Id });
            GridView1.DataSource = data;
            GridView1.DataBind();            
        }

        protected void DdlGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            var grade = long.Parse(DdlGrade.SelectedValue);
            DdlSubject.DataSource = db.TE_Subject.Where(x => x.Grad_Id == grade);
            DdlSubject.DataTextField = "Subject_Name";
            DdlSubject.DataValueField = "Id";
            DdlSubject.DataBind();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var row = GridView1.Rows[int.Parse(e.CommandArgument.ToString())];
            var lbl = (Label)row.FindControl("LblSId");
            var id = long.Parse(lbl.Text);

            db.DeleteObject(db.Te_Teacher_Subject.Single(x => x.Id == id));
            db.SaveChanges();            
            fillSubjectsGrid();
        }
    }
}